//-----------------------------------------------------------------------
// <copyright file="BulkOrderDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class BulkOrderDAOBase
    {
        #region Common methods
        public virtual BulkOrder CreateBulkOrderFromReader(IDataReader reader)
        {
            BulkOrder item = new BulkOrder();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("BulkOrderID"))) item.BulkOrderID = (int)reader["BulkOrderID"];
                if (!reader.IsDBNull(reader.GetOrdinal("OrderDate"))) item.OrderDate = (DateTime)reader["OrderDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Type"))) item.Type = (byte)reader["Type"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductCode"))) item.ProductCode = (string)reader["ProductCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("Color"))) item.Color = (string)reader["Color"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartDate"))) item.StartDate = (DateTime)reader["StartDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalQtyOrdered"))) item.TotalQtyOrdered = (int)reader["TotalQtyOrdered"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalQtyCompleted"))) item.TotalQtyCompleted = (int)reader["TotalQtyCompleted"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalQtyInProgressBelow50P"))) item.TotalQtyInProgressBelow50P = (int)reader["TotalQtyInProgressBelow50P"];
                if (!reader.IsDBNull(reader.GetOrdinal("TotalQtyInProgressAbove50P"))) item.TotalQtyInProgressAbove50P = (int)reader["TotalQtyInProgressAbove50P"];
                if (!reader.IsDBNull(reader.GetOrdinal("EstimatedDate"))) item.EstimatedDate = (DateTime)reader["EstimatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("StockLastCheckedDate"))) item.StockLastCheckedDate = (DateTime)reader["StockLastCheckedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("StockLastCheckedQty"))) item.StockLastCheckedQty = (int)reader["StockLastCheckedQty"];
                if (!reader.IsDBNull(reader.GetOrdinal("FOB"))) item.FOB = (decimal)reader["FOB"];
                if (!reader.IsDBNull(reader.GetOrdinal("CompletedDate"))) item.CompletedDate = (DateTime)reader["CompletedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("SupplierCode"))) item.SupplierCode = (string)reader["SupplierCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Owner"))) item.Owner = (int)reader["Owner"];
                if (!reader.IsDBNull(reader.GetOrdinal("Catalogue"))) item.Catalogue = (string)reader["Catalogue"];
                if (!reader.IsDBNull(reader.GetOrdinal("Page"))) item.Page = (string)reader["Page"];
                if (!reader.IsDBNull(reader.GetOrdinal("Pack"))) item.Pack = (string)reader["Pack"];
                if (!reader.IsDBNull(reader.GetOrdinal("PackQty"))) item.PackQty = (int)reader["PackQty"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogID"))) item.CatalogID = (int)reader["CatalogID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Target"))) item.Target = (bool)reader["Target"];
                if (!reader.IsDBNull(reader.GetOrdinal("SalesDescription1"))) item.SalesDescription1 = (string)reader["SalesDescription1"];
                if (!reader.IsDBNull(reader.GetOrdinal("SalesDescription2"))) item.SalesDescription2 = (string)reader["SalesDescription2"];
                if (!reader.IsDBNull(reader.GetOrdinal("StockLocation"))) item.StockLocation = (string)reader["StockLocation"];
                if (!reader.IsDBNull(reader.GetOrdinal("AvailableInUSA"))) item.AvailableInUSA = (bool)reader["AvailableInUSA"];
                if (!reader.IsDBNull(reader.GetOrdinal("BONo"))) item.BONo = (string)reader["BONo"];
				if (!reader.IsDBNull(reader.GetOrdinal("MinOrderQtyValue"))) item.MinOrderQtyValue = (int)reader["MinOrderQtyValue"];
                if (!reader.IsDBNull(reader.GetOrdinal("MinOrderQty"))) item.MinOrderQty = (string)reader["MinOrderQty"];
				if (!reader.IsDBNull(reader.GetOrdinal("MinQtyVolume"))) item.MinQtyVolume = (decimal)reader["MinQtyVolume"];
                if (!reader.IsDBNull(reader.GetOrdinal("TallPaletQty"))) item.TallPaletQty = (string)reader["TallPaletQty"];
				if (!reader.IsDBNull(reader.GetOrdinal("UnitSettingID"))) item.UnitSettingID = (int)reader["UnitSettingID"];
                if (!reader.IsDBNull(reader.GetOrdinal("IDSSortID"))) item.IDSSortID = (int)reader["IDSSortID"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsArchived"))) item.IsArchived = (bool)reader["IsArchived"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatPackDesc"))) item.CatPackDesc = (string)reader["CatPackDesc"];
                if (!reader.IsDBNull(reader.GetOrdinal("MUKTHGCatPackQty"))) item.MUKTHGCatPackQty = (int)reader["MUKTHGCatPackQty"];
                if (!reader.IsDBNull(reader.GetOrdinal("MUKTHGCatPackDesc"))) item.MUKTHGCatPackDesc = (string)reader["MUKTHGCatPackDesc"];
                if (!reader.IsDBNull(reader.GetOrdinal("HomeDelivered15Price"))) item.HomeDelivered15Price = (decimal)reader["HomeDelivered15Price"];
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyID"))) item.CurrencyID = (int)reader["CurrencyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsShownInLGDCatalog"))) item.IsShownInLGDCatalog = (bool)reader["IsShownInLGDCatalog"];
                if (!reader.IsDBNull(reader.GetOrdinal("SpecialOfferDisc"))) item.SpecialOfferDisc = (decimal)reader["SpecialOfferDisc"];
				if (!reader.IsDBNull(reader.GetOrdinal("QtyUnitsPer40Cont"))) item.QtyUnitsPer40Cont = (int)reader["QtyUnitsPer40Cont"];
				if (!reader.IsDBNull(reader.GetOrdinal("QtyUnitsPer20Cont"))) item.QtyUnitsPer20Cont = (int)reader["QtyUnitsPer20Cont"];
				if (!reader.IsDBNull(reader.GetOrdinal("UnitWeight"))) item.UnitWeight = (decimal)reader["UnitWeight"];
				if (!reader.IsDBNull(reader.GetOrdinal("HCC_Code"))) item.HCC_Code = (string)reader["HCC_Code"];
				if (!reader.IsDBNull(reader.GetOrdinal("TerritoryID"))) item.TerritoryID = (int)reader["TerritoryID"];
				if (!reader.IsDBNull(reader.GetOrdinal("ModifiedDate"))) item.ModifiedDate = (DateTime)reader["ModifiedDate"];

				if (!reader.IsDBNull(reader.GetOrdinal("UnitsPerOuter"))) item.UnitsPerOuter = (int)reader["UnitsPerOuter"];
				if (!reader.IsDBNull(reader.GetOrdinal("UnitFOBPrice"))) item.UnitFOBPrice = (decimal)reader["UnitFOBPrice"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBulkOrderFromReaderException, ex);
            }
            return item;
        }

		public virtual ProductDimensions CreateProductDimensionsFromReader(IDataReader reader)
		{
			ProductDimensions item = new ProductDimensions();
			try
			{
				if (!reader.IsDBNull(reader.GetOrdinal("DimensionID"))) item.DimensionID = (int)reader["DimensionID"];
				if (!reader.IsDBNull(reader.GetOrdinal("BulkOrderID"))) item.BulkOrderID = (int)reader["BulkOrderID"];
				if (!reader.IsDBNull(reader.GetOrdinal("IsProductRound"))) item.IsProductRound = (bool)reader["IsProductRound"];		
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS1"))) item.DiaOrLenS1 = (decimal)reader["DiaOrLenS1"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS2"))) item.DiaOrLenS2 = (decimal)reader["DiaOrLenS2"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS3"))) item.DiaOrLenS3 = (decimal)reader["DiaOrLenS3"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS4"))) item.DiaOrLenS4 = (decimal)reader["DiaOrLenS4"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS5"))) item.DiaOrLenS5 = (decimal)reader["DiaOrLenS5"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS6"))) item.DiaOrLenS6 = (decimal)reader["DiaOrLenS6"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS7"))) item.DiaOrLenS7 = (decimal)reader["DiaOrLenS7"];
				if (!reader.IsDBNull(reader.GetOrdinal("DiaOrLenS8"))) item.DiaOrLenS8 = (decimal)reader["DiaOrLenS8"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS1"))) item.HeightS1 = (decimal)reader["HeightS1"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS2"))) item.HeightS2 = (decimal)reader["HeightS2"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS3"))) item.HeightS3 = (decimal)reader["HeightS3"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS4"))) item.HeightS4 = (decimal)reader["HeightS4"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS5"))) item.HeightS5 = (decimal)reader["HeightS5"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS6"))) item.HeightS6 = (decimal)reader["HeightS6"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS7"))) item.HeightS7 = (decimal)reader["HeightS7"];
				if (!reader.IsDBNull(reader.GetOrdinal("HeightS8"))) item.HeightS8 = (decimal)reader["HeightS8"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS1"))) item.WidthS1 = (decimal)reader["WidthS1"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS2"))) item.WidthS2 = (decimal)reader["WidthS2"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS3"))) item.WidthS3 = (decimal)reader["WidthS3"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS4"))) item.WidthS4 = (decimal)reader["WidthS4"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS5"))) item.WidthS5 = (decimal)reader["WidthS5"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS6"))) item.WidthS6 = (decimal)reader["WidthS6"];		
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS7"))) item.WidthS7 = (decimal)reader["WidthS7"];
				if (!reader.IsDBNull(reader.GetOrdinal("WidthS8"))) item.WidthS8 = (decimal)reader["WidthS8"];	
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateProductDimensionFromReaderException, ex);
			}
			return item;
		}

        #endregion
        
        #region CreateBulkOrder methods
            
        public virtual void CreateBulkOrder(BulkOrder bulkOrder)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrdersInsert");
                
                dbCommandWrapper.AddInParameter("@OrderDate", DbType.DateTime, bulkOrder.OrderDate);
                dbCommandWrapper.AddInParameter("@Type", DbType.Byte, bulkOrder.Type);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, bulkOrder.ProductCode);
                dbCommandWrapper.AddInParameter("@Description", DbType.String, bulkOrder.Description);
                dbCommandWrapper.AddInParameter("@Color", DbType.String, bulkOrder.Color);
                dbCommandWrapper.AddInParameter("@StartDate", DbType.DateTime, bulkOrder.StartDate);
                dbCommandWrapper.AddInParameter("@TotalQtyOrdered", DbType.Int32, bulkOrder.TotalQtyOrdered);
                dbCommandWrapper.AddInParameter("@TotalQtyCompleted", DbType.Int32, bulkOrder.TotalQtyCompleted);
                dbCommandWrapper.AddInParameter("@TotalQtyInProgressBelow50P", DbType.Int32, bulkOrder.TotalQtyInProgressBelow50P);
                dbCommandWrapper.AddInParameter("@TotalQtyInProgressAbove50P", DbType.Int32, bulkOrder.TotalQtyInProgressAbove50P);
                dbCommandWrapper.AddInParameter("@EstimatedDate", DbType.DateTime, bulkOrder.EstimatedDate);
                dbCommandWrapper.AddInParameter("@StockLastCheckedDate", DbType.DateTime, bulkOrder.StockLastCheckedDate);
                dbCommandWrapper.AddInParameter("@StockLastCheckedQty", DbType.Int32, bulkOrder.StockLastCheckedQty);
                dbCommandWrapper.AddInParameter("@FOB", DbType.Currency, bulkOrder.FOB);
                dbCommandWrapper.AddInParameter("@CompletedDate", DbType.DateTime, bulkOrder.CompletedDate);
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, bulkOrder.SupplierCode);
                dbCommandWrapper.AddInParameter("@Owner", DbType.Int32, bulkOrder.Owner);
                dbCommandWrapper.AddInParameter("@Catalogue", DbType.String, bulkOrder.Catalogue);
                dbCommandWrapper.AddInParameter("@Page", DbType.AnsiString, bulkOrder.Page);
                dbCommandWrapper.AddInParameter("@Pack", DbType.AnsiString, bulkOrder.Pack);
                dbCommandWrapper.AddInParameter("@PackQty", DbType.Int32, bulkOrder.PackQty);
                dbCommandWrapper.AddInParameter("@CatalogID", DbType.Int32, bulkOrder.CatalogID);
                dbCommandWrapper.AddInParameter("@Target", DbType.Boolean, bulkOrder.Target);
                dbCommandWrapper.AddInParameter("@SalesDescription1", DbType.String, bulkOrder.SalesDescription1);
                dbCommandWrapper.AddInParameter("@SalesDescription2", DbType.String, bulkOrder.SalesDescription2);
                dbCommandWrapper.AddInParameter("@StockLocation", DbType.AnsiString, bulkOrder.StockLocation);
                dbCommandWrapper.AddInParameter("@AvailableInUSA", DbType.Boolean, bulkOrder.AvailableInUSA);
                dbCommandWrapper.AddInParameter("@BONo", DbType.AnsiString, bulkOrder.BONo);
				dbCommandWrapper.AddInParameter("@MinOrderQtyValue", DbType.Int32, bulkOrder.MinOrderQtyValue==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.MinOrderQtyValue);
                dbCommandWrapper.AddInParameter("@MinOrderQty", DbType.AnsiString, bulkOrder.MinOrderQty);
				dbCommandWrapper.AddInParameter("@MinQtyVolume", DbType.Currency, bulkOrder.MinQtyVolume==Nulls.NullDecimal?DBNull.Value:(object)bulkOrder.MinQtyVolume);
                dbCommandWrapper.AddInParameter("@TallPaletQty", DbType.AnsiString, bulkOrder.TallPaletQty);
				dbCommandWrapper.AddInParameter("@UnitSettingID", DbType.Int32, bulkOrder.UnitSettingID);
                dbCommandWrapper.AddInParameter("@IDSSortID", DbType.Int32, bulkOrder.IDSSortID);
                dbCommandWrapper.AddInParameter("@IsArchived", DbType.Boolean, bulkOrder.IsArchived);
                dbCommandWrapper.AddInParameter("@CatPackDesc", DbType.AnsiString, bulkOrder.CatPackDesc);
                dbCommandWrapper.AddInParameter("@MUKTHGCatPackQty", DbType.Int32, bulkOrder.MUKTHGCatPackQty);
                dbCommandWrapper.AddInParameter("@MUKTHGCatPackDesc", DbType.AnsiString, bulkOrder.MUKTHGCatPackDesc);
                dbCommandWrapper.AddInParameter("@HomeDelivered15Price", DbType.Currency, bulkOrder.HomeDelivered15Price);
                dbCommandWrapper.AddInParameter("@CurrencyID", DbType.Int32, bulkOrder.CurrencyID);
                dbCommandWrapper.AddInParameter("@IsShownInLGDCatalog", DbType.Boolean, bulkOrder.IsShownInLGDCatalog);
                dbCommandWrapper.AddInParameter("@SpecialOfferDisc", DbType.Currency, bulkOrder.SpecialOfferDisc);
				dbCommandWrapper.AddInParameter("@QtyUnitsPer40Cont", DbType.Int32, bulkOrder.QtyUnitsPer40Cont==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.QtyUnitsPer40Cont);
				dbCommandWrapper.AddInParameter("@QtyUnitsPer20Cont", DbType.Int32,bulkOrder.QtyUnitsPer20Cont==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.QtyUnitsPer20Cont);
				dbCommandWrapper.AddInParameter("@UnitWeight", DbType.Currency, bulkOrder.UnitWeight==Nulls.NullDecimal?DBNull.Value:(object)bulkOrder.UnitWeight);
				dbCommandWrapper.AddInParameter("@HCC_Code", DbType.AnsiString,  bulkOrder.HCC_Code == string.Empty?DBNull.Value:(object)bulkOrder.HCC_Code);
                dbCommandWrapper.AddInParameter("@ModifiedDate", DbType.DateTime, bulkOrder.ModifiedDate);

				dbCommandWrapper.AddInParameter("@UnitsPerOuter", DbType.Int32, bulkOrder.UnitsPerOuter==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.UnitsPerOuter);
				dbCommandWrapper.AddInParameter("@UnitFOBPrice", DbType.Currency, bulkOrder.UnitFOBPrice==Nulls.NullDecimal?DBNull.Value:(object)bulkOrder.UnitFOBPrice);

			    dbCommandWrapper.AddOutParameter("@BulkOrderID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                bulkOrder.BulkOrderID = (int)dbCommandWrapper.GetParameterValue("@BulkOrderID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateBulkOrderException, ex);
            }
        }

        #endregion

        #region UpdateBulkOrder methods
        
        public virtual void UpdateBulkOrder(BulkOrder bulkOrder)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrdersUpdate");            
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrder.BulkOrderID);
                dbCommandWrapper.AddInParameter("@OrderDate", DbType.DateTime, bulkOrder.OrderDate);
                dbCommandWrapper.AddInParameter("@Type", DbType.Byte, bulkOrder.Type);
                dbCommandWrapper.AddInParameter("@ProductCode", DbType.AnsiString, bulkOrder.ProductCode);
                dbCommandWrapper.AddInParameter("@Description", DbType.String, bulkOrder.Description);
                dbCommandWrapper.AddInParameter("@Color", DbType.String, bulkOrder.Color);
                dbCommandWrapper.AddInParameter("@StartDate", DbType.DateTime, bulkOrder.StartDate);
                dbCommandWrapper.AddInParameter("@TotalQtyOrdered", DbType.Int32, bulkOrder.TotalQtyOrdered);
                dbCommandWrapper.AddInParameter("@TotalQtyCompleted", DbType.Int32, bulkOrder.TotalQtyCompleted);
                dbCommandWrapper.AddInParameter("@TotalQtyInProgressBelow50P", DbType.Int32, bulkOrder.TotalQtyInProgressBelow50P);
                dbCommandWrapper.AddInParameter("@TotalQtyInProgressAbove50P", DbType.Int32, bulkOrder.TotalQtyInProgressAbove50P);
                dbCommandWrapper.AddInParameter("@EstimatedDate", DbType.DateTime, bulkOrder.EstimatedDate);
                dbCommandWrapper.AddInParameter("@StockLastCheckedDate", DbType.DateTime, bulkOrder.StockLastCheckedDate);
                dbCommandWrapper.AddInParameter("@StockLastCheckedQty", DbType.Int32, bulkOrder.StockLastCheckedQty);
                dbCommandWrapper.AddInParameter("@FOB", DbType.Currency, bulkOrder.FOB);
                dbCommandWrapper.AddInParameter("@CompletedDate", DbType.DateTime, bulkOrder.CompletedDate);
                dbCommandWrapper.AddInParameter("@SupplierCode", DbType.AnsiString, bulkOrder.SupplierCode);
                dbCommandWrapper.AddInParameter("@Owner", DbType.Int32, bulkOrder.Owner);
                dbCommandWrapper.AddInParameter("@Catalogue", DbType.String, bulkOrder.Catalogue);
                dbCommandWrapper.AddInParameter("@Page", DbType.AnsiString, bulkOrder.Page);
                dbCommandWrapper.AddInParameter("@Pack", DbType.AnsiString, bulkOrder.Pack);
                dbCommandWrapper.AddInParameter("@PackQty", DbType.Int32, bulkOrder.PackQty);
                dbCommandWrapper.AddInParameter("@CatalogID", DbType.Int32, bulkOrder.CatalogID);
                dbCommandWrapper.AddInParameter("@Target", DbType.Boolean, bulkOrder.Target);
                dbCommandWrapper.AddInParameter("@SalesDescription1", DbType.String, bulkOrder.SalesDescription1);
                dbCommandWrapper.AddInParameter("@SalesDescription2", DbType.String, bulkOrder.SalesDescription2);
                dbCommandWrapper.AddInParameter("@StockLocation", DbType.AnsiString, bulkOrder.StockLocation);
                dbCommandWrapper.AddInParameter("@AvailableInUSA", DbType.Boolean, bulkOrder.AvailableInUSA);
                dbCommandWrapper.AddInParameter("@BONo", DbType.AnsiString, bulkOrder.BONo);
				dbCommandWrapper.AddInParameter("@MinOrderQtyValue", DbType.Int32, bulkOrder.MinOrderQtyValue==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.MinOrderQtyValue);
                dbCommandWrapper.AddInParameter("@MinOrderQty", DbType.AnsiString, bulkOrder.MinOrderQty);
				dbCommandWrapper.AddInParameter("@MinQtyVolume", DbType.Currency, bulkOrder.MinQtyVolume==Nulls.NullDecimal?DBNull.Value:(object)bulkOrder.MinQtyVolume);
                dbCommandWrapper.AddInParameter("@TallPaletQty", DbType.AnsiString, bulkOrder.TallPaletQty);
				dbCommandWrapper.AddInParameter("@UnitSettingID", DbType.Int32, bulkOrder.UnitSettingID);
                dbCommandWrapper.AddInParameter("@IDSSortID", DbType.Int32, bulkOrder.IDSSortID);
                dbCommandWrapper.AddInParameter("@IsArchived", DbType.Boolean, bulkOrder.IsArchived);
                dbCommandWrapper.AddInParameter("@CatPackDesc", DbType.AnsiString, bulkOrder.CatPackDesc);
                dbCommandWrapper.AddInParameter("@MUKTHGCatPackQty", DbType.Int32, bulkOrder.MUKTHGCatPackQty);
                dbCommandWrapper.AddInParameter("@MUKTHGCatPackDesc", DbType.AnsiString, bulkOrder.MUKTHGCatPackDesc);
                dbCommandWrapper.AddInParameter("@HomeDelivered15Price", DbType.Currency, bulkOrder.HomeDelivered15Price);
                dbCommandWrapper.AddInParameter("@CurrencyID", DbType.Int32, bulkOrder.CurrencyID);
                dbCommandWrapper.AddInParameter("@IsShownInLGDCatalog", DbType.Boolean, bulkOrder.IsShownInLGDCatalog);
                dbCommandWrapper.AddInParameter("@SpecialOfferDisc", DbType.Currency, bulkOrder.SpecialOfferDisc);
				dbCommandWrapper.AddInParameter("@QtyUnitsPer40Cont", DbType.Int32, bulkOrder.QtyUnitsPer40Cont==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.QtyUnitsPer40Cont);
				dbCommandWrapper.AddInParameter("@QtyUnitsPer20Cont", DbType.Int32,bulkOrder.QtyUnitsPer20Cont==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.QtyUnitsPer20Cont);
				dbCommandWrapper.AddInParameter("@UnitWeight", DbType.Currency, bulkOrder.UnitWeight==Nulls.NullDecimal?DBNull.Value:(object)bulkOrder.UnitWeight);
				dbCommandWrapper.AddInParameter("@HCC_Code", DbType.AnsiString,  bulkOrder.HCC_Code == string.Empty?DBNull.Value:(object)bulkOrder.HCC_Code);
                dbCommandWrapper.AddInParameter("@ModifiedDate", DbType.DateTime, bulkOrder.ModifiedDate);
				dbCommandWrapper.AddInParameter("@UnitsPerOuter", DbType.Int32, bulkOrder.UnitsPerOuter==Nulls.NullInteger?DBNull.Value:(object)bulkOrder.UnitsPerOuter);
				dbCommandWrapper.AddInParameter("@UnitFOBPrice", DbType.Currency, bulkOrder.UnitFOBPrice==Nulls.NullDecimal?DBNull.Value:(object)bulkOrder.UnitFOBPrice);
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateBulkOrderException, ex);
            }
        }
        
        #endregion

        #region DeleteBulkOrder methods
        public virtual void DeleteBulkOrder(int bulkOrderID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrdersDelete");
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteBulkOrderException, ex);
            }
        }

        #endregion

        #region GetBulkOrder methods
        
        public virtual BulkOrder GetBulkOrder(int bulkOrderID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrdersGet");
                
                dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderID);
                
                BulkOrder bulkOrder =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        bulkOrder = CreateBulkOrderFromReader(reader);
                        reader.Close();
                    }
                }
                return bulkOrder;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBulkOrderException, ex);
            }
        }
        
        #endregion

        #region GetBulkOrders methods
        public virtual BulkOrderCollection GetBulkOrders(BulkOrderColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spBulkOrdersGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                BulkOrderCollection bulkOrderCollection = new BulkOrderCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        BulkOrder bulkOrder = CreateBulkOrderFromReader(reader);
                        bulkOrderCollection.Add(bulkOrder);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return bulkOrderCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetBulkOrdersException, ex);
            }
        }
        
        public virtual BulkOrderCollection GetBulkOrders(BulkOrderColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetBulkOrders(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

		#region ProductDimensions methods
		public virtual ProductDimensions GetProductDimensions( int bulkOrderID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spProductDimensionsGet");
                
				dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, bulkOrderID);
				                
				ProductDimensions productDimensions =  null;
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					if (reader.Read())
					{
						productDimensions = CreateProductDimensionsFromReader(reader);
						reader.Close();
					}
				}
				return productDimensions;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetProductDimensionsException, ex);
			}
		}
		#endregion

		#region ProductDimensions methods
		public virtual void CreateOrUpdateProductDimensions(ProductDimensions productDimension)
		{
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spProductDimensionsInsertOrUpdate");
                
				dbCommandWrapper.AddInParameter("@BulkOrderID", DbType.Int32, productDimension.BulkOrderID);
				dbCommandWrapper.AddInParameter("@IsProductRound", DbType.Boolean, productDimension.IsProductRound);
				dbCommandWrapper.AddInParameter("@DiaOrLenS1", DbType.Currency, productDimension.DiaOrLenS1==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS1);
				dbCommandWrapper.AddInParameter("@DiaOrLenS2", DbType.Currency, productDimension.DiaOrLenS2==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS2);
				dbCommandWrapper.AddInParameter("@DiaOrLenS3", DbType.Currency, productDimension.DiaOrLenS3==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS3);
				dbCommandWrapper.AddInParameter("@DiaOrLenS4", DbType.Currency, productDimension.DiaOrLenS4==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS4);
				dbCommandWrapper.AddInParameter("@DiaOrLenS5", DbType.Currency, productDimension.DiaOrLenS5==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS5);
				dbCommandWrapper.AddInParameter("@DiaOrLenS6", DbType.Currency, productDimension.DiaOrLenS6==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS6);
				dbCommandWrapper.AddInParameter("@DiaOrLenS7", DbType.Currency, productDimension.DiaOrLenS7==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS7);
				dbCommandWrapper.AddInParameter("@DiaOrLenS8", DbType.Currency, productDimension.DiaOrLenS8==Nulls.NullDecimal?DBNull.Value:(object)productDimension.DiaOrLenS8);

                dbCommandWrapper.AddInParameter("@HeightS1", DbType.Currency, productDimension.HeightS1==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS1);
				dbCommandWrapper.AddInParameter("@HeightS2", DbType.Currency, productDimension.HeightS2==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS2);
				dbCommandWrapper.AddInParameter("@HeightS3", DbType.Currency, productDimension.HeightS3==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS3);
				dbCommandWrapper.AddInParameter("@HeightS4", DbType.Currency, productDimension.HeightS4==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS4);
				dbCommandWrapper.AddInParameter("@HeightS5", DbType.Currency, productDimension.HeightS5==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS5);
				dbCommandWrapper.AddInParameter("@HeightS6", DbType.Currency, productDimension.HeightS6==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS6);
				dbCommandWrapper.AddInParameter("@HeightS7", DbType.Currency, productDimension.HeightS7==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS7);
				dbCommandWrapper.AddInParameter("@HeightS8", DbType.Currency, productDimension.HeightS8==Nulls.NullDecimal?DBNull.Value:(object)productDimension.HeightS8);

				dbCommandWrapper.AddInParameter("@WidthS1", DbType.Currency, productDimension.WidthS1==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS1);
				dbCommandWrapper.AddInParameter("@WidthS2", DbType.Currency, productDimension.WidthS2==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS2);
				dbCommandWrapper.AddInParameter("@WidthS3", DbType.Currency, productDimension.WidthS3==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS3);
				dbCommandWrapper.AddInParameter("@WidthS4", DbType.Currency, productDimension.WidthS4==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS4);
				dbCommandWrapper.AddInParameter("@WidthS5", DbType.Currency, productDimension.WidthS5==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS5);
				dbCommandWrapper.AddInParameter("@WidthS6", DbType.Currency, productDimension.WidthS6==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS6);
				dbCommandWrapper.AddInParameter("@WidthS7", DbType.Currency, productDimension.WidthS7==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS7);
				dbCommandWrapper.AddInParameter("@WidthS8", DbType.Currency, productDimension.WidthS8==Nulls.NullDecimal?DBNull.Value:(object)productDimension.WidthS8);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
				
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessCreateProductDimensionException, ex);
			}
		}

		#endregion

    }
}